package com.timschimandle.getitdone;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

public class ToDoListActivity extends FragmentActivity implements GeneralListFragment.OnItemSelectedListener {
	
	TaskDbAccess db;
	boolean mDualPane;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.to_do_list_activity);
		
		//check for the dual pane XML file
		View rightHolder = this.findViewById(R.id.right_holder);
		mDualPane = rightHolder !=null && rightHolder.getVisibility() == View.VISIBLE;
		
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		
		if (mDualPane) {
			
			TaskListFragment tlf = new TaskListFragment();
			ft.add(R.id.right_holder, tlf);
		}
				
		GeneralListFragment glf = new GeneralListFragment();
		ft.add(R.id.left_holder,glf).commit();
		
		db = new TaskDbAccess(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_to_do_list, menu);
		return true;
	}
	
	/*radio button on click method swapping fragments*/
	public void onRadioButtonClicked(View view) {
		
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		GeneralListFragment glf = new GeneralListFragment();
		Bundle bundle = new Bundle();
		
		switch (view.getId()) {
			case R.id.task_list_button:
				//refresh the list in the other fragment with the appropriate data
				
				//in dual pane refresh the fragment with the appropriate view
				if (mDualPane) {
					TaskListFragment tlf = new TaskListFragment();
					ft.replace(R.id.right_holder, tlf).commit();
				}
				break;
			case R.id.tasks:
				
				if (mDualPane) {
					TaskFragment tf = new TaskFragment();
					ft.replace(R.id.right_holder, tf);
				}
				
				bundle.putSerializable("tasks", db.getAllTasks());
				glf.setArguments(bundle);
				ft.replace(R.id.left_holder, glf).commit();
				
				break;
			case R.id.people:
								
				if (mDualPane) {
					PeopleFragment pf = new PeopleFragment();
					ft.replace(R.id.right_holder, pf);
				}
				
				bundle.putSerializable("people", db.getAllPeople());
				glf.setArguments(bundle);
				ft.replace(R.id.left_holder, glf).commit();
				
				break;
		}
		
		
	}
	
	/*iterate the number selector up*/
	public void up(View view) {
		
		int count;
		TextView tv;
		
		switch(view.getId()) {
			case R.id.up_day:
				tv = (TextView) this.findViewById(R.id.day_number);
				count = Integer.valueOf(tv.getText().toString());
				if (count == 31) {
					count = 0;
				} else {
					count += 1;
				}
				tv.setText(Integer.toString(count));
				break;
			case R.id.up_month:
				tv = (TextView) this.findViewById(R.id.month_number);
				count = Integer.valueOf(tv.getText().toString());
				if (count == 12) {
					count = 0;
				} else {
					count += 1;
				}
				tv.setText(Integer.toString(count));
				break;
			case R.id.up_year:
				tv = (TextView) this.findViewById(R.id.year_number);
				count = Integer.valueOf(tv.getText().toString());
				if (count == 25) {
					count = 0;
				} else {
					count += 1;
				}
				tv.setText(Integer.toString(count));
				break;
		}
	}
	
	/*iterate the number selector down*/
	public void down(View view) {
		
		int count;
		TextView tv;
		
		switch(view.getId()) {
			case R.id.down_day:
				tv = (TextView) this.findViewById(R.id.day_number);
				count = Integer.valueOf(tv.getText().toString());
				if (count == 0) {
					count = 31;
				} else {
					count -= 1;
				}
				tv.setText(Integer.toString(count));
				break;
			case R.id.down_month:
				tv = (TextView) this.findViewById(R.id.month_number);
				count = Integer.valueOf(tv.getText().toString());
				if (count == 0) {
					count = 12;
				} else {
					count -= 1;
				}
				tv.setText(Integer.toString(count));
				break;
			case R.id.down_year:
				tv = (TextView) this.findViewById(R.id.year_number);
				count = Integer.valueOf(tv.getText().toString());
				if (count == 0) {
					count = 25;
				} else {
					count -= 1;
				}
				tv.setText(Integer.toString(count));
				break;
		}
	}
	
	/*save task method*/
	public void saveTask(View view) {
		//take the input boxes and create a new task from them
		Long id = Long.valueOf(-1);
		String title = ((EditText) this.findViewById(R.id.task_title)).getText().toString();
		int day = Integer.parseInt(((TextView) this.findViewById(R.id.day_number)).getText().toString());
		int month = Integer.parseInt(((TextView) this.findViewById(R.id.month_number)).getText().toString());
		int year = Integer.parseInt(((TextView) this.findViewById(R.id.year_number)).getText().toString());
		/* the person is the id of their database entry */
		Long assigned = Long.valueOf(1234);
		boolean active = ((CheckBox) this.findViewById(R.id.is_active)).isChecked();
		
		Task t = new Task(id, title, day, month, year, assigned, active);
		db.addTask(t);
	}

	public void savePerson(View view) {
		Long id = Long.valueOf(-1);
		String f = ((EditText)this.findViewById(R.id.first_name)).getText().toString();
		String l = ((EditText)this.findViewById(R.id.last_name)).getText().toString();
		String em = ((EditText)this.findViewById(R.id.email)).getText().toString();
		String pn = ((EditText)this.findViewById(R.id.phone_number)).getText().toString();
		
		Person p = new Person(id, f, l, em, pn);
		db.addPerson(p);
	}

	@Override
	public void onItemSelectedListener(Long id) {
		// TODO Auto-generated method stub
		RadioGroup rg = (RadioGroup) this.findViewById(R.id.radio_data);
		Bundle bundle = new Bundle();
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		
		
		if (rg.getCheckedRadioButtonId() == R.id.task_list_button) {
			//get task list item to put in the other fragment
		} else if (rg.getCheckedRadioButtonId() == R.id.tasks) {
			
			TaskFragment fragment = new TaskFragment();
			
			//check if the ID is -1 which indicates a new entry for that list.
			if (id != -1) {
				Task t = db.getTask(id);
				bundle.putSerializable("selected_task", t);
				fragment.setArguments(bundle);
			}

			//if dual pane replace the right_holder, if left then replace the left_holder
			
			if (mDualPane) {
				ft.replace(R.id.right_holder, fragment).commit();
			} else {
				ft.replace(R.id.left_holder, fragment).commit();
			}
			
		} else if (rg.getCheckedRadioButtonId() == R.id.people) {
			
			PeopleFragment fragment = new PeopleFragment();
			
			if (id != -1) {
				Person p = db.getPerson(id);
				bundle.putSerializable("selected_person", p);
				fragment.setArguments(bundle);
			}
			
			if (mDualPane) {
				ft.replace(R.id.right_holder, fragment).commit();
			} else {
				ft.replace(R.id.left_holder, fragment).commit();
			}
		}
	}
}
